package com.skp.shaphan.ui.buttons;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.actions.ExportDataAction;
import com.skp.shaphan.ui.dialogs.ExportDialog;
import com.skp.shaphan.ui.results.IResultPane;
import com.skp.shaphan.ui.results.ResultPane;

/**
 * This button exports data from a screen.  This is deprecated and can be removed.
 * Use the ExportDataAction instead.
 * 
 * @see ExportDataAction
 * @author Stephen Phillips
 * @deprecated
 *
 */
public class ExportOkButton extends JButton implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3198756123987599845L;
	private ExportDialog dialog;
	public ExportOkButton(ExportDialog dialog) {
		super("Ok");
		this.dialog = dialog;
		this.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("exporting...");
		IResultPane resultPane = ShaphanApp.getMainWindow().getResultPane();
		String sqlStatement = resultPane.getCurrentSQLStatement();
		try {
			ResultSet results = buildResults(resultPane.getResultSet().getStatement().getConnection(), sqlStatement);
			if(dialog.isFileExport()) {
				ResultsToFile(results);
			} else {
				ResultsToClipboard(results);
			}
		} catch (SQLException e1) {
			(new ProjectJobException(e1)).displayException();
		} catch (FileNotFoundException e1) {
			(new ProjectJobException(e1)).displayException();
		}
		dialog.dispose();
	}
	
	private ResultSet buildResults(Connection conn, String sql) throws SQLException {
		System.out.println("building results...");
		PreparedStatement st = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
		System.out.println("prepared statement...");
		ResultSet results = st.executeQuery();
		System.out.println("results complete...");
		return results;
	}
	
	private void ResultsToFile(ResultSet rs) throws SQLException, FileNotFoundException {
		File outputFile = new File(dialog.getOutputPath());
		PrintWriter out = new PrintWriter(new FileOutputStream(outputFile));
		
		String line;
		ResultSetMetaData meta = rs.getMetaData();
		while(rs.next()) {
			line = "";
			for(int x=1; x<=meta.getColumnCount(); x++) {
				if(x > 1) {
					line += "\t";
				}
				if(rs.getObject(x) == null) {
					line += "<null>";
				} else {
					line += rs.getObject(x).toString();
				}
			}
			out.println(line);
		}
		Statement st = rs.getStatement();
		rs.close();
		st.close();

		out.close();
	}

	private void ResultsToClipboard(ResultSet rs) throws SQLException {
		String output = "";
		ResultSetMetaData meta = rs.getMetaData();
		while(rs.next()) {
			for(int x=1; x<=meta.getColumnCount(); x++) {
				if(x > 1) {
					output += "\t";
				}
				if(rs.getObject(x) == null) {
					output += "<null>";
				} else {
					output += rs.getObject(x).toString();
				}
			}
			output += "\n";
		}
		Statement st = rs.getStatement();
		rs.close();
		st.close();
		
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		StringSelection contents = new StringSelection(output);
		clipboard.setContents(contents, ShaphanApp.getMainWindow().getClipboard());
	}
}
